package service.Impl;

import bean.Area;
import dao.IAreaDao;
import dao.impl.AreaDaoImpl;
import org.apache.commons.beanutils.BeanUtils;
import service.IAreaService;
import util.PageUtil;
import vo.AreaVO;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class AreaServiceImpl implements IAreaService {
    IAreaDao iad =new AreaDaoImpl();
    @Override
    public PageUtil<AreaVO> listByPage(int pageNo, int pageSize) {

        List<Area> areas = iad.listByPage(pageNo,pageSize);
        List<AreaVO> areaVOS =new ArrayList<>();
        for (Area area:areas) {
            AreaVO vo =new AreaVO();
            try {
                BeanUtils.copyProperties(vo,area);
                if(area.getClassB()!=null){
                    BeanUtils.copyProperty(vo,"f_className",area.getClassB().getF_className());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            areaVOS.add(vo);

        }
        int count =iad.getCount();
        PageUtil<AreaVO> pages =new PageUtil<>(areaVOS,count,pageNo,pageSize);
        return pages;
    }

    @Override
    public PageUtil<AreaVO> listByPage(int pageNo, int pageSize, String f_areaName, String f_forestType, String f_className) {

        List<Area> areas =iad.listByPage(pageNo,pageSize,f_areaName,f_forestType,f_className);
        List<AreaVO> areaVOS =new ArrayList<>();
        for (Area area:areas) {
            AreaVO vo =new AreaVO();
            try {
                BeanUtils.copyProperties(vo,area);
                if(area.getClassB()!=null){
                    BeanUtils.copyProperty(vo,"f_className",area.getClassB().getF_className());
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            areaVOS.add(vo);

        }

        int count =iad.getCount(f_areaName,f_forestType,f_className);
        PageUtil<AreaVO> pages =new PageUtil<>(areaVOS,count,pageNo,pageSize);
        return pages;
    }

    @Override
    public boolean addOneArea(Area a) {
        return iad.addOneArea(a)>0?true:false;
    }

    @Override
    public List<Area> getNoClassArea() {
        return iad.getNoClassArea();
    }

    @Override
    public Area findById(int id) {
        return iad.findById(id);
    }
}
